<?php
require_once("./define/define_table.php");
require_once("./class/PrintQuery.php");
define("ADD_ASSOCIATION_FORM", "Formulaire d'ajout d'une association");
define("MODIFY_ASSOCIATION_FORM", "Formulaire de modification d'une association");

class Association{
	
	private $id_association;
	private $nom;
	
	public function Association(){
	       
	}
	
	public function getNom(){
		return $this->nom;
	}
	
	public function getId(){
		return $this->id_association;
	}
	
	public function setNom($nom){
	    $this->nom = $nom;
	}
	
	public function setId($id){
	    $this->id_association = $id;
	}
	
	public function getAllAssociation(){
	    global $db;
		try{
			$sql = "SELECT id_association, nom FROM ".ASSOCIATION_TABLE;
			$sth = $db->prepare($sql);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Association");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public function getAssociation($id){
	    global $db;
	    try{
	        $sql = "SELECT id_association, nom FROM ".ASSOCIATION_TABLE." WHERE id_association = :id_association";
	        $sth = $db->prepare($sql);
	        $sth->bindValue(":id_association", $id, PDO::PARAM_INT);
	        $sth->execute();
	        return $sth->fetchObject("Association");
	    }catch(PDOException $e){
	        echo $e.getMessage();
	    }
	}
	
	public function addOrModify(){
		global $db;
		try{
			if($this->id_association == null){
				$sql = "INSERT INTO ".ASSOCIATION_TABLE." VALUES(null, :nom)";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
			}
			else{
				$sql = "UPDATE ".ASSOCIATION_TABLE." SET nom = :nom WHERE id_association= :id_association";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
				$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
			}
			PrintQuery::queryPrint($sth);
			return $sth->execute(); 
		}catch(PDOException $e){
			echo $e->getMessage();
		}
	}
	
	public static function getAddForm(){
		$html = ADD_ASSOCIATION_FORM .
				'<form action="?module=association&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>NOM</td>' .
				'			<td><input type="text" name="nom" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function getModifyForm(){
		$html = MODIFY_ASSOCIATION_FORM .
				'<form action="?module=association&action=doModify" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>ID</td>' .
				'			<td><input readonly type="text" name="id_association" value="'.$this->id_association.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>NOM</td>' .
				'			<td><input type="text" name="nom" value="'.$this->nom.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}

	public function delete(){
	    global $db;
	    $sql = "DELETE FROM ".ASSOCIATION_TABLE." WHERE id_association = :id_association";
	    $sth = $db->prepare($sql);
	    $sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
	    $sth->execute();
	    PrintQuery::queryPrint($sth);
	}
	
	public function generateSelect($id = null){
	    $array_association = self::getAllAssociation();
	    $html = '<select name="id_association">';
	    foreach($array_association as $a){
	        $selected = ($a->getId() == $id) ?  'selected="selected"' : '';
	        $html .= '<option value="'.$a->getId().'" '.$selected.'>'.$a->getNom().'</option>';
	    }
	    $html .= '</select>';
	    return $html;
	}
}
?>
